package org.esisar.jdbc;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public enum JDBCConnector {

	JDBCConnector;
	
	private final Properties prop;

	private JDBCConnector() {
		this.prop = new Properties();

		try {
			
			InputStream in = getClass().getResourceAsStream("jdbc.properties");
			
			prop.load(in);
			String driver = prop.getProperty("driver");
			Class.forName(driver);
			
		} catch (IOException |ClassNotFoundException e ) {
			e.printStackTrace();
		}
	}


	public Connection connect() throws SQLException{


		String dbname = prop.getProperty("dbname");
		String login = prop.getProperty("login");
		String password = prop.getProperty("password");
		String server = prop.getProperty("server");
		String port = prop.getProperty("port");

		Connection pol;
		try {
			pol = DriverManager.getConnection("jdbc:oracle:thin:@"+ server + ":" + port + ":" + dbname, login, password);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new SQLException(e);	//TODO - do it better
		}

		return pol;
	}


	public static void main(String[] args) {
		try {

			JDBCConnector.JDBCConnector.connect();
			
			System.out.println("Dzila");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}